﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ext.Net;
using System.IO;

namespace Sio.Mdm.Web.Controls
{
    public class TabPanel : Ext.Net.TabPanel
    {
        public TabPanel()
        {
            EnableTabScroll = true;
        }

        #region Properties

        #region Public

        #region Urls
        private List<string> _pages;
        /// <summary>
        /// Array of urls to use for pages
        /// </summary>
        public List<string> Urls
        {
            get
            {
                if (null == _pages)
                {
                    _pages = new List<string>();
                }

                return _pages;
            }
        }   
        #endregion

        #endregion

        #endregion

        #region Methods

        #region Protected
        protected override void OnLoad(EventArgs e)
        {
            if (!ExtNet.IsAjaxRequest)
            {
                // Used with selectors to find parent
                Style.Add("tabParent", "true");                
                GeneratePages();
            }

            base.OnLoad(e);
        }
        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            writer.AddAttribute("tabParent", "true");
            base.RenderBeginTag(writer);
        }        
        #endregion

        #region Private
        private void GeneratePages()
        {
            foreach (string pageUrl in Urls)
            {
                GenerateTab(pageUrl);
            }
        }

        private void GenerateTab(string tabUrl)
        {
            Panel tab = new Panel();

            tab.AutoLoad.ShowMask = true;
            tab.AutoLoad.Mode = LoadMode.IFrame;
            tab.AutoLoad.Url = tabUrl;
            tab.LazyMode = LazyMode.Instance;
            tab.AutoLoad.ShowMask = true;
            tab.AutoLoad.TriggerEvent = "activate";

            string[] parameters = new string[0];

            if (tabUrl.Contains('?'))
            {
                int index = tabUrl.IndexOf('?');
                
                parameters = tabUrl.Substring(index + 1).Split('&');
                tabUrl = tabUrl.Substring(0, index);
            }
            
            tab.Border = false;
            tab.Title = GetTitle(tabUrl);
            tab.ID = String.Format("{0}_{1}", ID, Items.Count);
            tab.CustomConfig.Add(new ConfigItem("parentTabId", ID, ParameterMode.Value));

            Items.Add(tab);
        } 
        #endregion

        #region Internal
        internal string GetTitle(string url)
        {
            try
            {
                using (StreamReader reader = File.OpenText(Page.MapPath(url)))
                {
                    string content = reader.ReadToEnd();

                    int start = content.IndexOf("<title>") + 7;
                    int end = content.IndexOf("</title>");

                    if (start > -1 && start < end)
                    {
                        return content.Substring(start, end - start);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

            return url;
        }  
        #endregion

        #endregion
    }
}
